var selectedPage = 0;
var total = 0;
var total_paginas = 0;

jQuery(document).ready(function () {
    loadMain();
});
function loadMain() {
    doTimer();

    loadSettings();

    System.Gadget.settingsUI = "settings.html";
    System.Gadget.onShowSettings = loadSettings;
    System.Gadget.onSettingsClosing = settingsClosing;
    System.Gadget.onSettingsClosed = settingsClosed;
    System.Gadget.onUndock = checkState;
    System.Gadget.onDock = checkState;

    checkState();

    startTimer(UpdateFeed, 1000);
}
function settingsClosed(event) {
    if (event.closeAction == event.Action.commit) {
        selectedPage = 0;
        loadSettings();
        checkState();
    }
}
function checkState() {
    UpdatePaging();
}
function UpdatePaging() {
    if (System.Gadget.docked) {
        if ($('#body').hasClass('big')) {
            $('#body').removeClass('big');
        }
        if (!$('#body').hasClass('small')) {
            $('#body').addClass('small');
        }

        with (document.body.style) {
            width = "154px";
        }
    }
    else if (!System.Gadget.docked) {
        if ($('#body').hasClass('small')) {
            $('#body').removeClass('small');
        }
        if (!$('#body').hasClass('big')) {
            $('#body').addClass('big');
        }

        with (document.body.style) {
            width = "294px";
        }
    }

    if ($('#body').hasClass('loading')) {
        $("div#bg").height(68);
        $("div#conteudo div#meio").height(54);

        with (document.body.style) {
            height = "185px";
        }
    } else {
        total = items.length;
        if (total % pageSize == 0) {
            total_paginas = total / pageSize;
        }
        else {
            total_paginas = total / pageSize + 1;
        }
        total_paginas = Math.floor(total_paginas);

        $("div#bg").height(pageSize * 60 - 12);
        $("div#conteudo div#meio").height(pageSize * 60 - 26);

        with (document.body.style) {
            height = (105 + pageSize * 60) + "px";
        }

        $('div#conteudo div#meio div#items').empty();
        for (var i = selectedPage * pageSize; i < total && i < (selectedPage + 1) * pageSize; i++) {
            var link = getNode('link', i);
            var title = getNode('title', i);
            var smallTitle = title;

            if ($('#body').hasClass('big')) {
                smallTitle = smallTitle.length > 122 ? smallTitle.substr(0, 122) + "..." : smallTitle;
            }
            else {
                smallTitle = smallTitle.length > 52 ? smallTitle.substr(0, 52) + "..." : smallTitle;
            }

            $('div#conteudo div#meio div#items').append(
                '<div class="item"><a href="' +
                link +
                '" title="' +
                title +
                '" target="_blank">' +
                smallTitle +
                '</a></div>'
            );
        }
        if (selectedPage == 0) {
            if (!$('#prev').attr('disabled')) {
                $('#prev').attr('disabled', 'disabled');
                $('#prev').addClass('disabled');
            }
        }
        else {
            if ($('#prev').attr('disabled')) {
                $('#prev').removeAttr('disabled');
            }
            if ($('#prev').hasClass('disabled')) {
                $('#prev').removeClass('disabled');
            }
        }
        if (selectedPage == total_paginas - 1) {
            if (!$('#next').attr('disabled')) {
                $('#next').attr('disabled', 'disabled');
                $('#next').addClass('disabled');
            }
        }
        else {
            if ($('#next').attr('disabled')) {
                $('#next').removeAttr('disabled');
            }
            if ($('#next').hasClass('disabled')) {
                $('#next').removeClass('disabled');
            }
        }
        $('#pg').empty();
        $('#pg').append(
            (selectedPage + 1) +
            '&nbsp;-&nbsp;' +
            total_paginas
        );
    }
}
function Prev() {
    if (selectedPage > 0) {
        selectedPage--;
        UpdatePaging();
    }
}
function Next() {
    if (selectedPage < total_paginas - 1) {
        selectedPage++;
        UpdatePaging();
    }
}
function UpdateFeed() {
    if (ReadRSS('http://www.porta25.com.br/blog/feed/rss/')) {
        flag = true;
        if ($('#body').hasClass('loading')) {
            $('#body').removeClass('loading');
        }
        
        UpdatePaging();
        startTimer(UpdateFeed, 1800000);
    }
    else {
        startTimer(UpdateFeed, 30000);
    }
}
var g_timer;
function startTimer(f, time) {
    stopTimer();
    g_timer = setInterval(f, time);
}
function stopTimer() {
    clearInterval(g_timer);
}